package com.kx.service_exam.service.impl;

import com.kx.service_exam.dao.ModulePartRealMapper;
import com.kx.service_exam.dao.ModulePartRealMapper_My;
import com.kx.service_exam.exception.BusinessException;
import com.kx.service_exam.model.ExaminationPart;
import com.kx.service_exam.model.ModulePartReal;
import com.kx.service_exam.service.interfaces.IExaminationPartSV;
import com.kx.service_exam.service.interfaces.IModulePartRealSV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ModulePartRealSVImpl implements IModulePartRealSV {

    private static Set<String> cashModulePartReal = new HashSet<String>();

    @Autowired
    private ModulePartRealMapper modulePartRealMapper;

    @Autowired
    private ModulePartRealMapper_My modulePartRealMapper_my;

    @Autowired
    private IExaminationPartSV examinationPartSV;

    @Override
    public int insertRecord(ModulePartReal record) {
        return modulePartRealMapper.insertSelective(record);
    }

    @Override
    public int deleteRecord(String id) {
        return modulePartRealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateRecord(ModulePartReal record) {
        return modulePartRealMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ModulePartReal selectRecordById(String id) {
        return modulePartRealMapper.selectByPrimaryKey(id);
    }

    @Override
    public int batchInsertModulePartReal(List<ModulePartReal> modulePartReals, String operatorId, String examId) {
        if(modulePartReals == null || modulePartReals.isEmpty()) {
            throw new BusinessException("参数[modulePartReals]为null");
        }
        if(StringUtils.isEmpty(examId)) {
            throw new BusinessException("参数[examId]为null");

        }
        Iterator<ModulePartReal> iterator = modulePartReals.iterator();
        while (iterator.hasNext()){
            ModulePartReal modulePartReal = iterator.next();
            if(!checkModulePartReal(modulePartReal)){
                iterator.remove();
                continue;
            }
            Date date = new Date();
            modulePartReal.setId(UUID.randomUUID().toString());
            modulePartReal.setDelete_flag(0);
            modulePartReal.setCreate_time(date);
            modulePartReal.setModify_time(date);
            modulePartReal.setCreator(operatorId);
            modulePartReal.setModifier(operatorId);
            modulePartReal.setExam_id(examId);
        }
        return modulePartRealMapper_my.batchInsertModulePartReal(modulePartReals);
    }

    /**
     * 过滤没有用的记录和参数不对的
     * @param modulePartReal
     * @return
     */
    private boolean checkModulePartReal(ModulePartReal modulePartReal){
        boolean hasModulePartReal= false;
        // 单选题部分
        if(!StringUtils.isEmpty(modulePartReal.getChoice_depart_id())) {
            if(this.checkPartId(modulePartReal.getChoice_depart_id())){
                hasModulePartReal = true;
            } else {
                return false;
            }
        }

        //多选题部分
        if(!StringUtils.isEmpty(modulePartReal.getMulti_choice_depart_id())) {
            if(this.checkPartId(modulePartReal.getMulti_choice_depart_id())){
                hasModulePartReal = true;
            } else {
                return false;
            }
        }

        //填空题部分
        if(!StringUtils.isEmpty(modulePartReal.getFill_depart_id())) {
            if(this.checkPartId(modulePartReal.getFill_depart_id())){
                hasModulePartReal = true;
            } else {
                return false;
            }
        }

        //判断题部分
        if(!StringUtils.isEmpty(modulePartReal.getJudge_part_id())) {
            if(this.checkPartId(modulePartReal.getJudge_part_id())){
                hasModulePartReal = true;
            } else {
                return false;
            }
        }

        //问答题部分
        if(!StringUtils.isEmpty(modulePartReal.getEssay_part_id())) {
            if(this.checkPartId(modulePartReal.getEssay_part_id())){
                hasModulePartReal = true;
            } else {
                return false;
            }
        }

        //最后判断是否有关系记录
        if( !hasModulePartReal ){
            return false;
        }
        return true;
    }

    private boolean checkPartId (String partId){
        if(cashModulePartReal.contains(partId)) return true;
        ExaminationPart examinationPart = examinationPartSV.selectRecordById(partId);
        if(examinationPart !=null ) {
            cashModulePartReal.add(partId);
            return true;
        }
        return false;
    }
}
